<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="changePassword" #changePasswordForm="ngForm">
  <mat-toolbar color="primary">
    <h2 translate>{{ data?.changeDefaultPassword ? 'profile.change-default-password' : 'profile.change-password' }}</h2>
    <span class="flex flex-1"></span>
    <button mat-icon-button
      [mat-dialog-close]="false"
      type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  @if (isLoading$ | async) {
    <mat-progress-bar color="warn" mode="indeterminate">
    </mat-progress-bar>
  }
  @if (!(isLoading$ | async)) {
    <div style="height: 4px;"></div>
  }
  <div mat-dialog-content>
    <div class="change-password-container" tb-toast toastTarget="changePassword">
      <div class="change-password">
        <div class="flex flex-row gap-6 xs:flex-col xs:gap-0">
          <div class="flex min-w-[350px] flex-col xs:max-w-full">
            <h3 class="card-title" translate>profile.change-password</h3>
            <mat-form-field [class.!hidden]="data?.changeDefaultPassword" class="mat-block">
              <mat-label translate>profile.current-password</mat-label>
              <input matInput type="password" formControlName="currentPassword"/>
              <mat-icon class="material-icons" matPrefix>lock</mat-icon>
              <tb-toggle-password matSuffix></tb-toggle-password>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.incorrect-password-try-again' | translate }}"
                        [class.!hidden]="!changePassword.get('currentPassword').hasError('differencePassword')"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
            <mat-form-field class="mat-block" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>login.new-password</mat-label>
              <input matInput type="password" formControlName="newPassword"/>
              <mat-icon class="material-icons" matPrefix>lock</mat-icon>
              <tb-toggle-password matSuffix></tb-toggle-password>
              <mat-hint></mat-hint>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-not-meet-requirements' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword').errors
                                          && !changePassword.get('newPassword').hasError('hasWhitespaces')
                                          && !changePassword.get('newPassword').hasError('samePassword')
                                          && changePassword.get('newPassword').touched)"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="changePassword.get('newPassword').getError('alreadyUsed')"
                        [class.!hidden]="!changePassword.get('newPassword').hasError('alreadyUsed')"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-should-difference' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword').hasError('samePassword') && changePassword.get('newPassword').touched)"
                        class="tb-error">
                warning
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.password-should-not-contain-spaces' | translate }}"
                        [class.!hidden]="!changePassword.get('newPassword').hasError('hasWhitespaces')"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
            <mat-form-field class="mat-block" appearance="outline" subscriptSizing="dynamic">
              <mat-label translate>login.new-password-again</mat-label>
              <input matInput type="password" name="new-password" formControlName="newPassword2" autocomplete="new-password" required/>
              <mat-icon class="material-icons" matPrefix>lock</mat-icon>
              <tb-toggle-password [class.!hidden]="!(changePassword.get('newPassword2').dirty || changePassword.get('newPassword2').touched)" matSuffix></tb-toggle-password>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'security.password-requirement.new-passwords-not-match' | translate }}"
                        [class.!hidden]="!(changePassword.get('newPassword2').hasError('differencePassword') && changePassword.get('newPassword2').touched)"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
          </div>
          <mat-divider [vertical]="true"></mat-divider>
          <div class="flex flex-1 xs:hidden">
            @if (passwordPolicy) {
              <div class="password-requirements">
                <h3 class="card-title" translate>security.password-requirement.password-requirements</h3>
                <h4 class="mat-h4" translate>security.password-requirement.at-least</h4>
                @if (passwordPolicy.minimumUppercaseLetters > 0) {
                  <p class="mat-body">
                    <tb-icon class="tb-mat-20">
                      {{ changePassword.get('newPassword').hasError('notUpperCase') ? 'mdi:circle-small' : 'mdi:check' }}
                    </tb-icon>
                    {{ 'security.password-requirement.uppercase-letter' | translate : {count: passwordPolicy.minimumUppercaseLetters} }}
                  </p>
                }
                @if (passwordPolicy.minimumLowercaseLetters > 0) {
                  <p class="mat-body">
                    <tb-icon class="tb-mat-20">
                      {{ changePassword.get('newPassword').hasError('notLowerCase') ? 'mdi:circle-small' : 'mdi:check' }}
                    </tb-icon>
                    {{ 'security.password-requirement.lowercase-letter' | translate : {count: passwordPolicy.minimumLowercaseLetters} }}
                  </p>
                }
                @if (passwordPolicy.minimumDigits > 0) {
                  <p class="mat-body">
                    <tb-icon class="tb-mat-20">
                      {{ changePassword.get('newPassword').hasError('notNumeric') ? 'mdi:circle-small' : 'mdi:check' }}
                    </tb-icon>
                    {{ 'security.password-requirement.digit' | translate : {count: passwordPolicy.minimumDigits} }}
                  </p>
                }
                @if (passwordPolicy.minimumSpecialCharacters > 0) {
                  <p class="mat-body">
                    <tb-icon class="tb-mat-20">
                      {{ changePassword.get('newPassword').hasError('notSpecial') ? 'mdi:circle-small' : 'mdi:check' }}
                    </tb-icon>
                    {{ 'security.password-requirement.special-character' | translate : {count: passwordPolicy.minimumSpecialCharacters} }}
                  </p>
                }
                @if (passwordPolicy.minimumLength > 0) {
                  <p class="mat-body">
                    <tb-icon class="tb-mat-20">
                      {{ changePassword.get('newPassword').hasError('minLength') ? 'mdi:circle-small' : 'mdi:check' }}
                    </tb-icon>
                    {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.minimumLength} }}
                  </p>
                }
                @if (passwordPolicy.maximumLength > 0) {
                  <div class="password-requirements">
                    <h4 class="mat-h4" translate>security.password-requirement.at-most</h4>
                    <p class="mat-body">
                      <mat-icon class="tb-mat-20" [svgIcon]="changePassword.get('newPassword').hasError('maxLength') ? 'mdi:circle-small' : 'mdi:check'"></mat-icon>
                      {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.maximumLength} }}
                    </p>
                  </div>
                }
              </div>
            }
          </div>
        </div>
      </div>
    </div>
  </div>
  <div mat-dialog-actions class="flex flex-row items-center justify-end">
    <mat-slide-toggle (change)="notShowAgain = !notShowAgain" style="margin: 0 8px;">
      {{ 'action.dont-show-again' | translate}}
    </mat-slide-toggle>
    <span class="flex flex-1"></span>
    @if (data) {
      <button mat-button color="primary"
        (click)="onSkip()"
        type="button"
        [disabled]="(isLoading$ | async)"
        cdkFocusInitial>
        {{ 'action.skip' | translate }}
      </button>
    }
    <button mat-raised-button color="primary"
      (click)="onChangePassword(changePasswordForm)"
      [disabled]="(isLoading$ | async) || changePassword.invalid">
      {{ 'profile.change-password' | translate }}
    </button>
  </div>
</form>
